package artid.com.arttogoa.volley;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import artid.com.arttogoa.parser.AppItem;
import artid.com.arttogoa.util.NetUtils;

import android.text.TextUtils;
import android.util.Log;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author 熊彬
 * @data: 2016-3-11 下午3:42:28
 * @version: V1.0
 */
public class VolleyData {
	/**
	 * TODO<请描述这个方法是干什么的>
	 * 
	 * @throw
	 * @return
	 */
	public final static int Empty = -3;
	public final static int Error = -2;
	public final static int TimeOut = 0;
	public final static int Failed = -1;
	public final static int Success = 1;

	private int code = Error;
	private String msg;
	private String json;

	private JSONObject obj;
	private JSONArray arr;
	private final Class<? extends AppItem> cls;

	private AppItem data;
	private List<AppItem> list = new ArrayList<AppItem>();

	public JSONArray getArr() {
		return arr;
	}

	public void setArr(JSONArray arr) {
		this.arr = arr;
	}

	public AppItem getData() {
		return data;
	}

	public void setData(AppItem data) {
		this.data = data;
	}

	public List<? extends AppItem> getList() {
		return list;
	}

	public void setList(List<AppItem> list) {
		this.list = list;
	}

	// public JSONObject getData() {
	// return data;
	// }
	//
	// public void setData(JSONObject data) {
	// this.data = data;
	// }
	private void tryParse(String json) {
		// System.out.println("HTML:"+json);
		if (TextUtils.isEmpty(json)) {
			setCode(Error);
			setMsg("联网失败，请检查网络连接");
			return;
		}
		try {
			obj = new JSONObject(json);
			parseStatus(obj);
		} catch (JSONException e) {
			setCode(Failed);
			setMsg("请检查网络连接");
			e.printStackTrace();
		}
	}

	private void parseStatus(JSONObject obj) {
		if (obj == null) {
			return;
		}
		System.out.println("obj.optInt(ret)=" + obj.optInt("ret"));
		msg = obj.optString("msg");
		code = obj.optInt("ret");
	}

	public VolleyData(String d, Class<? extends AppItem> cls) {
		// TODO Auto-generated constructor stub
		this.cls = cls;
		json = NetUtils.decodeUnicode(d);
		tryParse(json);
		if (getCode() == Success) {
			Object obj = getObject(getObj());
			if (obj == null) {
				return;
			}
			//你想练的话给个简单的
			if (obj instanceof JSONObject) {
				parseData((JSONObject) obj);
			}
			if (obj instanceof JSONArray) {
				parseList((JSONArray) obj);
			}
		}
	}

	private Object getObject(JSONObject obj) {
		if (cls == AppItem.class) {// 基础参数
			return obj;
		}
		if (obj.optJSONObject("data") != null
				&& obj.optJSONArray("data") == null) {
			return obj.optJSONObject("data");
		} else if (obj.optJSONObject("data") == null
				&& obj.optJSONArray("data") != null) {
			return obj.optJSONArray("data");
		} else if (obj.optJSONObject("data") == null
				&& obj.optJSONArray("data") == null) {
			VolleyBasic.disMissLoding();
			Log.e("解析json错误！！！！！同时含有arr和obj", obj.toString());
			return obj;
		} else {
			return obj;
		}
	}

	private void parseList(JSONArray array) {
		if (cls == AppItem.class) {
			return;
		}
		if (null == array || array.length() == 0) {
			return;
		}
		int len = array.length();
		for (int i = 0; i < len; i++) {
			JSONObject obj = array.optJSONObject(i);
			try {
				AppItem item = (AppItem) cls.newInstance();
				item.parse(obj);
				// System.out.println(obj.toString());
				list.add(item);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	private void parseData(JSONObject obj) {
		if (cls == AppItem.class) {
			return;
		}
		if (obj == null) {
			return;
		}
		try {
			data = (AppItem) cls.newInstance();
			data.parse(obj);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	// private Object getObject(JSONObject obj) {
	// if (obj == null) {
	// return null;
	// }
	// msg = obj.optString("msg");
	// code = obj.optInt("ret");
	// Object dest = null;
	// JSONObject ob = obj.optJSONObject("data");
	// JSONArray arr = obj.optJSONArray("data");
	// if (ob != null) {
	// dest = ob;
	// } else if (arr != null) {
	// dest = arr;
	// }
	// return dest;
	// }

	public JSONObject getObj() {
		return obj;
	}

	// private void parseList(JSONArray obj) {
	//
	// setData(obj.optJSONObject("data"));
	// }
	//
	// private void parseData(JSONObject obj) {
	// if (obj == null) {
	// return;
	// }
	// setData(obj.optJSONObject("data"));
	// }

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

}
